#include "Scene/PlantChooseScene.h"
#include "AudioEngine.h"
#include "ui/UIButton.h"
#include "ui/CocosGUI.h"
#include"Scene/GameScene.h"
#include "Other/Data.h"
USING_NS_CC;
Scene* PlantChooseScene::createScene() {
    auto plant_choose_scene = PlantChooseScene::create();

    return plant_choose_scene;
}
// 游戏选择界面
bool PlantChooseScene::init() {

    if (!Scene::init()) {
        return false;
    }
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    //音乐播放
    AudioEngine::preload("Music/Choosing.mp3");
    auto choose_BGM = AudioEngine::play2d("Music/Choosing.mp3", true);
    //创建背景图 并且设置坐标为左起点
    auto background_sprite = Sprite::create("Games/bg1.jpg");
    background_sprite->setPosition(visibleSize.width / 2 + 255, visibleSize.height / 2);
    background_sprite->setScale(1.32f);
    this->addChild(background_sprite,0);
    //移动动画
    auto background_move_to = MoveTo::create(3.0f, Vec2(visibleSize.width / 2 - 255, visibleSize.height / 2));
    background_sprite->runAction(background_move_to);
    auto delay = DelayTime::create(3.0f); // 延时动作
    //植物表  动画
    auto choose_table_sprite = Sprite::create("Games/PanelBackground1.png");
    choose_table_sprite->setPosition(visibleSize.width / 2 + 130, visibleSize.height / 2 - 600); // moveto  : visibleSize.width / 2 + 130, visibleSize.height / 2 - 100
    choose_table_sprite->setScale(1.3f);
    background_sprite->addChild(choose_table_sprite,1);
    auto choose_table_move_to = MoveTo::create(0.5f, Vec2(visibleSize.width / 2 + 130, visibleSize.height / 2 - 100));
    auto table_sequence = Sequence::create( delay, choose_table_move_to, nullptr);
    choose_table_sprite->runAction(table_sequence);
    //卡片选择带  动画
    auto card_bar_sprite = Sprite::create("Games/ChooserBackground.png");
    card_bar_sprite->setPosition(visibleSize.width / 2 + 160, visibleSize.height / 2 + 200); // moveto  : visibleSize.width / 2 + 150, visibleSize.height / 2 + 120
    card_bar_sprite->setScale(1.3f);
    background_sprite->addChild(card_bar_sprite, 1);
    auto card_bar_move_to = MoveTo::create(0.5f, Vec2(visibleSize.width / 2 + 150, visibleSize.height / 2 + 130));
    auto card_sequence = Sequence::create(delay, card_bar_move_to, nullptr);
    card_bar_sprite->runAction(card_sequence);
    //确认按钮 ui::button  转到GameScene 
    //确认按钮需要确定选择植物的个数  个数足够才能按确定
    auto delay2 = DelayTime::create(3.5f); 
    auto switch_to_GameScene = ui::Button::create("Games/StartButton.png");
    switch_to_GameScene->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type)
        {
            switch (type)
            {
            case ui::Widget::TouchEventType::BEGAN:
                break;
            case ui::Widget::TouchEventType::ENDED:
                // 选择界面BGM停止
                AudioEngine::uncache("Music/Choosing.mp3");
                AudioEngine::stop(choose_BGM);
                auto GameScene = GameScene::create();
                //Sleep(1); 
                Director::getInstance()->pushScene(GameScene);
                break;
            }
        });
    switch_to_GameScene->setScale(1.85f);
    switch_to_GameScene->setPosition(Vec2(visibleSize.width * 0.28f-1000.0f, visibleSize.height * 0.085f-1000.0f));
    auto choose_table_move_to2 = MoveTo::create(0.001f, Vec2(visibleSize.width * 0.28f, visibleSize.height * 0.085f));
    auto table_sequence2 = Sequence::create(delay2, choose_table_move_to2, nullptr);
    switch_to_GameScene->runAction(table_sequence2);
    this->addChild(switch_to_GameScene);
    //僵尸等待动画
    NormalZombie* normal_zombie1 = new NormalZombie; // 普通僵尸
    normal_zombie1->sprite_init( Vec2(880, 300));
    normal_zombie1->sprite->setScale(1.2f);
    background_sprite->addChild(normal_zombie1->sprite, 3);
    NormalZombie* normal_zombie = new NormalZombie; // 普通僵尸
    normal_zombie->sprite_init(Vec2(900, 100));
    normal_zombie->sprite->setScale(1.2f);
    background_sprite->addChild(normal_zombie->sprite, 3);

    ConeheadZombie* conehead_zombie1 = new ConeheadZombie; // 路障僵尸
    conehead_zombie1->sprite_init( Vec2(800, 300));
    conehead_zombie1->sprite->setScale(1.2f);
    background_sprite->addChild(conehead_zombie1->sprite, 3);
    ConeheadZombie* conehead_zombie = new ConeheadZombie; // 路障僵尸
    conehead_zombie->sprite_init( Vec2(950, 120));
    conehead_zombie->sprite->setScale(1.2f);
    background_sprite->addChild(conehead_zombie->sprite, 3);

    BucketheadZombie* buckethead_zombie1 = new BucketheadZombie; // 铁桶僵尸
    buckethead_zombie1->sprite_init(Vec2(870, 200));
    buckethead_zombie1->sprite->setScale(1.2f);
    background_sprite->addChild(buckethead_zombie1->sprite, 3);
    BucketheadZombie* buckethead_zombie = new BucketheadZombie; // 铁桶僵尸
    buckethead_zombie->sprite_init (Vec2(930, 350));
    buckethead_zombie->sprite->setScale(1.2f);
    background_sprite->addChild(buckethead_zombie->sprite, 3);

    //卡片精灵
    static std::vector<Card*> card_in_list;
    static SunFlowerCard* card1 = new SunFlowerCard;    //sunflower card
    card1->sprite_init("SunFlower", Vec2(80,-85));
    this->addChild(card1->sprite, 1);
    auto card1_move_to = MoveTo::create(0.5f, Vec2(80,415));
    auto card1_sequence = Sequence::create(delay, card1_move_to, nullptr);
    card1->sprite->runAction(card1_sequence);
    card_in_list.push_back(card1);    
    static PeashooterCard* card2 = new PeashooterCard;   //peashooter card
    card2->sprite_init("PeaShooter", Vec2(130, -85));
    this->addChild(card2->sprite, 1);
    auto card2_move_to = MoveTo::create(0.5f, Vec2(130, 415));
    auto card2_sequence = Sequence::create(delay, card2_move_to, nullptr);
    card2->sprite->runAction(card2_sequence);
    card_in_list.push_back(card2);
    static WallNutCard* card3 = new WallNutCard;     //wallnut card
    card3->sprite_init("WallNut", Vec2(180, -85));
    this->addChild(card3->sprite, 1);
    auto card3_move_to = MoveTo::create(0.5f, Vec2(180, 415));
    auto card3_sequence = Sequence::create(delay, card3_move_to, nullptr);
    card3->sprite->runAction(card3_sequence);
    card_in_list.push_back(card3);

    //卡片动画
    static int card_chosen = 0;
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [&](Touch* t, Event* e) {
        Rect range;
        auto card_move = MoveTo::create(0.1f, Vec2(135, 535));
        for (auto& c : card_in_list) {
            range =c->getRect();
            if (range.containsPoint(t->getLocation()))
            {
                if (c->can_choose) {
                    //log("touchcard");    //调试用
                    switch (c->card_type) {
                    case CardType::card_type_SunFlower:
                        //log("touchcard1");  //调试用
                        c->cd_sprite_init("SunFlowerCD", c->sprite->getPosition());
                        this->addChild(c->cd_sprite, 2);
                        card_move = MoveTo::create(0.1f, Vec2(135 + 50 * card_chosen, 555));
                        c->sprite->runAction(card_move);
                        card_chosen += 1;
                        break;
                    case CardType::card_type_Peashooter:
                        //log("touchcard2");
                        c->cd_sprite_init("PeashooterCD", c->sprite->getPosition());
                        this->addChild(c->cd_sprite, 2);
                        card_move = MoveTo::create(0.1f, Vec2(135 + 50 * card_chosen, 555));
                        c->sprite->runAction(card_move);
                        card_chosen += 1;
                        break;
                    case CardType::card_type_WallNut:
                        //log("touchcard3");
                        c->cd_sprite_init("WallNutCD", c->sprite->getPosition());
                        this->addChild(c->cd_sprite, 2);
                        card_move = MoveTo::create(0.1f, Vec2(135 + 50 * card_chosen, 555));
                        c->sprite->runAction(card_move);
                        card_chosen += 1;
                        break;
                    }
                }
                break;
            }
        }
        return true;
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    return true;
}


void PlantChooseScene::menuCloseCallback(Ref* pSender) {
    //Close the cocos2d-x game scene and quit the application
    Director::getInstance()->end();
}